/*
  esp8266
  NodeMCU 网页请求
  2021-09-14
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>

const char * URL = "https://api.seniverse.com/v3/weather/now.json?key=ScoJx_gW1ZljH9utJ&location=shenzhen&language=zh-Hans&unit=c";
#define ssid "TPLee" //"TPLeeSZ"
#define pwd  "6515304*eeL" //"2021#@Lee.com"

#define SeniverseFignure "a38e2711bef6d02a6e77cc2d48af94f5cc557676"

void setup() {
  Serial.begin(9600);

  WiFi.mode(WIFI_STA); // 工作模式为无线终端
  WiFi.begin(ssid, pwd); // 连接wifi
  
  while (WiFi.status() != WL_CONNECTED) // 等待连接
  {
    delay(1000);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("Wifi Connected");
  
  httpClientRequest();
}

void httpClientRequest()
{
  HTTPClient client;
  //WiFiClient wifiClient; //httpClient
  WiFiClientSecure httpsClient;
  httpsClient.setFingerprint(SeniverseFignure);
  httpsClient.setTimeout(15000);
  delay(1000);
  
  Serial.printf("URL: %s\n\n", URL);

  Serial.printf("connect server: %d\n", httpsClient.connect("api.seniverse.com",443));

  int httpCode = client.GET(); // 启动连接发送HTTP请求
  Serial.printf("Send GET request to %s resp %d\n\n", URL, httpCode);

  if (httpCode == HTTP_CODE_OK) // 返回成功
  {
    String resp = client.getString();
    Serial.printf("Server response %s\n\n", resp);
  }

  client.end();// 关闭服务器连接
  httpsClient.stop();
}

void loop() {
  // put your main code here, to run repeatedly:

}
